package wheel;

import java.util.LinkedHashMap;

public class Word {
	private int position;                  // holds the position of the word in the phrase
	private String text;                   // holds the text of the word so far, i.e., * and determined letters
	private String pattern;                // lower case for the regexp, upper case for letters that are determined
	private LinkedHashMap<String, Guess> guesses;  // holds the computer's guesses for the word
	private Boolean solved;                // set to TRUE if computer believes the word is solved
	
	public Word(int position, String text) {
		this.position = position;
		this.text = text;
		this.pattern = "";
		this.solved = false;
		LinkedHashMap<String, Guess> lhm = new LinkedHashMap<String, Guess>();
		this.guesses = lhm;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public LinkedHashMap<String, Guess> getGuesses() {
		return guesses;
	}

	@SuppressWarnings("unchecked")
	public void setGuesses(LinkedHashMap<String, Guess> guesses) {
		// Make a copy of the LinkedHashMap parameter to avoid
		// pass by reference issues, if guesses is reused by calling method.
		this.guesses = (LinkedHashMap<String, Guess>) guesses.clone();
	}

	public void addGuess(String text, Guess guess) {
		this.guesses.put(text, guess);
	}
	
	public Boolean getSolved() {
		return solved;
	}

	public void setSolved(Boolean solved) {
		this.solved = solved;
	}

	public int getLength() {
		return this.text.length();
	}

	/**
	 * Determine whether a given position in this word has already been solved or if it is still
	 * a variable. 
	 * @param position The position to check in the word
	 * @return true if the character is already known for sure, false otherwise
	 */
	public boolean charAlreadySolved(int position) {
		if (text.charAt(position) != '*')
			return true;
		return false;
	}
	
	public int getGuessCount() {
		return guesses.size();
	}
}
